<template>
  <n-card :bordered="false" class="proCard">

    <BasicForm @register="register" @submit="handleSubmit">
      <template #anchorSelect="{ model, field }">
        <n-input v-model:value="model[field]" :options="anchorOptions" clearable/>
      </template>
    </BasicForm>
    <BasicTable :columns="columns"
      :request="loadDataTable"
      :initRequest="false"
      :row-key="(row) => row.id"
      ref="actionRef"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090">
      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import {reactive, ref, unref, onMounted,} from 'vue';
  import {c, useMessage} from 'naive-ui';
  import {BasicTable} from '@/components/Table';
  import {columns, fromSchema} from './columns';
  import {useRouter} from 'vue-router';
  import {BasicForm, useForm} from '@/components/Form/index';
  import {getLiveBroadList, getLiveDetail, LiveDetailParams} from "@/api/liveBroadcast/anchorBill";
  import {formatToDate} from "@/utils/dateUtil";
  import {getChanList} from "@/api/game/chan";
  import {useUserStore} from "@/store/modules/user";
  import BillChart from "../billChart.vue"

  const message = useMessage();
  const userStore = useUserStore();
  const actionRef = ref();
  const showModal = ref(false);
  const registChanArray = ref([]);
  const anchorOptions = ref([]);
  const dataSource = ref([]);
  const tableLoading = ref(false);

  const formParams = reactive<LiveDetailParams>({
    anchorName: '',
    chanId: '',
    pageSize: 0
  });


  const [register, {setFieldsValue}] = useForm({
    gridProps: {cols: '1 s:1 m:2 l:3 xl:4 2xl:4'},
    labelWidth: 120,
    schemas: fromSchema
  });


  function onCheckedRow(rowKeys: any[]) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function dateDisableVaildate(current: number, phase: 'start' | 'end', value: [number, number] | null) {
    if (value != null) {
      const vArry = value as number[];
      let [startTime, endTime] = vArry;
      return endTime > new Date().getTime() || startTime > new Date().getTime()
    }

  }

  async function handleSubmit(values: Recordable) {
    console.log(values);
    if(values.anchorName === null) values.anchorName=''
    let tempParams = unref(formParams);
    Object.assign(tempParams, values);
    reloadTable()
  }

  const loadDataTable = async (res) => {
    const param = {...formParams, ...res};
    let ret = await getLiveDetail(param)
    console.log(ret)
    ret.total = ret.rows
    return ret;
  };


  onMounted(async () => {
    reloadTable()
  });

  async function loadDataSource() {
    tableLoading.value = true;
    try {
      const result = await getLiveDetail(fromParams);
      console.log('data', result)
      //const anchorInfo = result.anchorInfo;
      //const anchorData = result.chart;
      dataSource.value = unref([]);
      for(let i = 0; i < result.length; i++){   
        let tmp = []
        let dt = {
          ...result[i].anchorInfo,
          ...result[i].chart
        }
        tmp.push(result[i].anchorInfo)
        dataSource.value.push(dt)
      }
      dataSource.value = dataSource.value.reverse()
      console.log(anchorOptions.value)

    } finally {
      tableLoading.value = false;
    }
  }
</script>

<style lang="less" scoped>
</style>
